<?php
class editorHelper{
	//获取随机文件名称
	static function getRndFileName($sExt){
		$time = explode(' ', microtime());
		$rand = ssfbase::random(4);
		if(strlen($rand) < 4) $rand = str_repeat('0', 4 - strlen($rand)) . $rand;
		return strtoupper(base_convert($time[1] + $time[0], 10, 36) . $rand) . "." . $sExt;
	}
	//获取小图片文件
	static function getSmallImageFile($s_File, $e = '_s'){
		$exts = explode(".", $s_File);
		$ext = array_pop($exts);
		return implode('.',$exts) . $e .'.'. $ext;
	}
	//是否是图片
	static function isPic( $ext){
		$ext = strtolower($ext);
		$sAllowExt = "jpg|jpeg|png|gif|bmp";
		if ( in_array( $ext, explode('|', $sAllowExt))) return true;
		else return false;
	}

	
	static function exitupload($key){
		$map = array(
				1 => 'not-found',
				2 => 'invalid-upload',
				3 => 'size-too-big',
				4 => 'type-not-match',
				5 => 'move-error',
				6 => 'not-allowed',
				7 => 'It is not a picture',
				8 => 'It is too small'
		);
		$arr = array('status'=>404, "msg"=>$map[$key]);
		echo ssfbase::jsonencode($arr);
		exit();
	}

	static function getUploadDir($channelid = 0){
		global $channel;
		if( $channelid>0){
			$sSaveDir   =  'attachment/'.$channel[$channelid]['channeldir']."/";
		}else{
			$sSaveDir   =  "attachment/support/";
		}
		$mapping = array('Y/m', 'Y', 'Ym', 'Ymd' ,'Y/m' ,'Y/m/d');
		$dirType = intval($channel[$channelid]['upload_date_path']);
		$sCreateDir = isset($mapping[$dirType]) ? ssfbase::formatTime($mapping[$dirType]) : ssfbase::formatTime($mapping[0]);
		return $sSaveDir . $sCreateDir . "/";
	}

	function formatStringTime($s){
		if(!$s) return time();
		$a = array();
		$s = trim($s);
		$len = strlen($s);
		$index = 0;
		$start = true;
		for($i=0;$i<$len;$i++){
			$b = ord($s[$i]);
			if($b >= 48 && $b <= 57){
				if($start == false) $index++;
				$start = true;
				$a[$index][] = intval($s[$i]);
			}else{
				$start = false;
				continue;
			}
		}
		$arr = array();
		foreach($a as $val){
			$arr[] = implode('', $val);
		}
		$t = strtotime(intval($arr[0]) .'-'. intval($arr[1]) .'-'. intval($arr[2]) .' '. intval($arr[3]) .':'. intval($arr[4]) .':'. intval($arr[5]));
		return $t ? $t : time();
	}

	static function formaturl($l1, $l2) {
		if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i", $l1, $regs)) {
			foreach($regs[0] as $num => $url) {
				$l1 = str_replace($url, self::formaturlreplace($url, $l2), $l1);
			}
		}
		return $l1;
	}

	static function formaturlreplace($l1, $l2) {
		if (preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i", $l1, $regs)) {
			$I2 = $regs[3];
		}
		if (strlen($I2) > 0) {
			$I1 = str_replace(chr(34), "", $I2);
			$I1 = str_replace(chr(39), "", $I1);
		} else {
			return $l1;
		}
		$url_parsed = parse_url($l2);
		$scheme = $url_parsed["scheme"];
		if ($scheme != "") {
			$scheme = $scheme . "://";
		}
		$host = $url_parsed["host"];
		$l3 = $scheme . $host;
		if (strlen($l3) == 0) {
			return $l1;
		}
		$path = dirname($url_parsed["path"]);
		if ($path[0] == "\\") {
			$path = "";
		}
		$pos = strpos($I1, "#");
		if ($pos > 0) $I1 = substr($I1, 0, $pos);

		if (preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&amp;)|&)+/i", $I1)) {
			return $l1;
		} 
		elseif ($I1[0] == "/") {
			$I1 = $l3 . $I1;
		} 
		elseif (substr($I1, 0, 3) == "../") { 
			while (substr($I1, 0, 3) == "../") {
				$I1 = substr($I1, strlen($I1) - (strlen($I1)-3), strlen($I1)-3);
				if (strlen($path) > 0) {
					$path = dirname($path);
				}
			}
			$I1 = $l3 . $path . "/" . $I1;
		} elseif (substr($I1, 0, 2) == "./") {
			$I1 = $l3 . $path . substr($I1, strlen($I1) - (strlen($I1)-1), strlen($I1)-1);
		} elseif (strtolower(substr($I1, 0, 7)) == "mailto:" || strtolower(substr($I1, 0, 11)) == "javascript:") {
			return $l1;
		} else {
			$I1 = $l3 . $path . "/" . $I1;
		}
		return str_replace($I2, "\"$I1\"", $l1);
	}
}
?>
